/**
 * Q 8-5 WorkThreadPatternCountUp
 * @author andy.rong
 */
package com.myjavalab.thread;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class WorkThreadPatternCountUp {

	public static void main(String[] args) {
		System.out.println(Thread.currentThread().getName() + " : Begin");
		new CountUpFrame();
		System.out.println(Thread.currentThread().getName() + " : End");
	}
}

class CountUpFrame extends JFrame implements ActionListener{

	private static final long serialVersionUID = 132164L;
	
	private final JLabel label = new JLabel("Event dispatching thread example");
	private final JButton button = new JButton("Countup");
	public CountUpFrame() {
		super("CountUp");
		getContentPane().setLayout(new FlowLayout());
		getContentPane().add(label);
		getContentPane().add(button);
		button.addActionListener(this);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setVisible(true);
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == button) {
			countUp();
		}
	}
	
	private void countUp() {
		for(int i = 0; i < 10; i++) {
			System.out.println(Thread.currentThread().getName() + ":countUp:setText(" + i + ")");
			label.setText("" + i);
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){
				
			}
		}
	}
	
}